<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title></title>
	<script>
		function search() {
			// var $fid = fid.value;
			// if (!$fid) {
			// 	alert("编号不能为空");
			// 	return;
			// }
			var obj=new URLSearchParams(location.search);
			var $fid=obj.get("fid")
			//1.创建xhr异步对象
			var xhr = new XMLHttpRequest();
			//2.创建请求,打开连接
			xhr.open("GET", `/laptop_family/v1/laptop_family_search/${$fid}`, true)
			//3.发送请求
			xhr.send();
			//4.接收响应
			xhr.onreadystatechange = function () {
				if (xhr.readyState == 4 && xhr.status == 200) {
					var r = xhr.responseText;
					console.log(r)
					//解析响应结果
					if (r == 0) {
						alert("未查到")
					} else {
						var arr = JSON.parse(r);
						// sfid.value=arr[0].fid;
						fname.value=arr[0].fname;
					}
				}
			}
		}
		function revise() {
            //0.收集用户填入信息
            // var $fid = fid.value;
			var obj=new URLSearchParams(location.search);
			var $fid=obj.get("fid")
			// var $fid=sfid.value;
            var $fname = fname.value;
           
            //1.创建xhr异步对象
            var xhr = new XMLHttpRequest();
            //4.接收响应
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var r = xhr.responseText;
                    if (r == 1) {
                        alert("修改成功");
						location.href="laptop_family_get.html"
                    } else {
                        alert("修改失败")
                    }
                }
            }
            //2.创建请求,打开连接
            xhr.open("put", `/laptop_family/v1/laptop_family_revise`, true);
            //3.发送请求
            //3.1创建请求主体
            var formdata = `fid=${$fid}&fname=${$fname}`
            console.log(formdata);
            //3.2设置请求头信息
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send(formdata);
        }
	</script>
</head>

<body onload="search()">
	<!-- <input type="text" id="fid"><br> -->
	<!-- <button onclick="">查询</button><br> -->
	<!-- <input type="text" id="sfid"><br> -->
    <input type="text" id="fname">类别<br>
    <button onclick="revise()">修改</button>
</body>
</body>

</html>